#!/usr/bin/python

# visualize per host CPU, disk I/O, and memory information: 
# collected from linux /proc/ 
# cpu_per  		- (utime+stime)/time_interval 			(%)
# VmSize   		- total virtual memory size   			(MB)
# VmRSS    		- total occupied memory       			(MB)
# read_bytes 	- actual read bytes from storage level 	(KB) 	
# write_bytes 	- actual write bytes from storage level (MB)


import sys
import re


def getvalue(a):
	return a[a.find("=")+1:].strip("\"")


if len(sys.argv) <= 1:
	print "file name?\n"
	sys.exit(-1)

osinfo = {}
min = 100000000000000
for l in open(sys.argv[1]):
	A = l.split()
	time = 0
	rbytes = 0
	wbytes = 0
	msize = 0
	rss = 0
	cpu = 0.0
	for a in A:
		if a.startswith("walltime"):
			time = int(getvalue(a))
			if time < min:
				min = time
		if a.startswith("readbytes="):
			rbytes = int(getvalue(a))
		if a.startswith("writebytes="):
			wbytes = int(getvalue(a))
		if a.startswith("VmSize"):
			msize = int(getvalue(a))
		if a.startswith("VmRSS"):
			rss = int(getvalue(a))
		if a.startswith("cpu_per"):
			cpu = float(getvalue(a))
			if cpu > 8.0:
				cpu = 0.0

	# no such entry, then init
	if not osinfo.has_key(time):
		osinfo[time] = {}
		osinfo[time]["rbytes"] = 0 
		osinfo[time]["wbytes"] = 0
		osinfo[time]["msize"] = 0
		osinfo[time]["rss"] = 0
		osinfo[time]["cpu"] = 0.0

	osinfo[time]["rbytes"] += (rbytes/1024)
	osinfo[time]["wbytes"] += (wbytes/(1024*1024))
	osinfo[time]["msize"] += (msize/1024)
	osinfo[time]["rss"] += (rss/1024)
	osinfo[time]["cpu"] += (cpu/8)
		

mf = open('rb.dat', 'w')
for k,v in osinfo.items():
	mf.write(str(k-min)+' '+ str(v["rbytes"])+ '\n' )
mf.close()

mf = open('wb.dat', 'w')
for k,v in osinfo.items():
	mf.write(str(k-min)+' '+ str(v["wbytes"]) + '\n' )
mf.close()


mf1 = open('vm.dat', 'w')
for k,v in osinfo.items():
	mf1.write(str(k-min)+' '+str(v["msize"])+ '\n')
mf1.close()

mf1 = open('m.dat', 'w')
for k,v in osinfo.items():
	mf1.write(str(k-min)+' ' + str(v["rss"]) + '\n')
mf1.close()

rf1 = open('cpu.dat', 'w')
for k,v in osinfo.items():
	rf1.write(str(k-min)+' '+str(v["cpu"])+'\n')
rf1.close()


